
var app = getApp(); //获取全局

Page({
  /**
   * 页面的初始数据
   */
  data: {
    
  },
  onLoad: function(option) {
    var openId = wx.getStorageSync('useronlyData').openid;
    
    var postId = option.id;
    // this.setData({
    //   currentPostId: postId
    // })
    //将postId传入data中方便之后的方法调用
    var that = this
    this.data.currentPostId = postId
    this.data.openId = openId
    
   
    
    wx.request({
      url: 'http://localhost:8080/Comment/findByArticleId?articleId='+ that.data.currentPostId,
      method: 'GET',
      data:{},
      success: function(res){
       var comment_List = res.data.commentList;
        if (comment_List != null) {
          that.setData({
            commentList: comment_List
          })
        } else {

        }
      }

    })
    wx.request({
      url: 'http://localhost:8080/article/findById?id='+postId,
      method: 'GET',
      data: {
        
      },
      success: function (res) {
        var list = res.data.Article;
        var tags = res.data.tags;
        
        if (list == null ) {
          var toastText = '获取数据失败' + res.data.errMsg;
          console.log(toastText)
          wx.showToast({
            title: toastText,
            icon: 'none'
          })
        } else {
          that.setData({
            article : list
            
          })
        }
        if (tags == null) {
          var toastText = '暂无标签' + res.data.errMsg;
          console.log(toastText)
          
        } else {
          that.setData({
            tags: tags

          })
        }
      }
    })
    if (openId != undefined) {
      wx.request({
        url: 'http://localhost:8080/ArticleStar/findStarIsExsit?openId=' + that.data.openId + '&articleId=' + that.data.currentPostId,
        method: 'GET',
        data: {},
        success: function (res) {
          var isExsit = res.data;
          if (isExsit == false) {
            that.setData({
              collected: false
            })
          } else {
            wx.request({
              url: 'http://localhost:8080/ArticleStar/getStarStatus?openId=' + that.data.openId + '&articleId=' + that.data.currentPostId,
              method: 'GET',
              data: {},
              success: function (res) {
                var status_code = res.data;
                if (status_code == 1) {
                  that.setData({
                    collected: true
                  })
                } else if (status_code == 0) {
                  that.setData({
                    collected: false
                  })
                } else {
                  wx.showToast({
                    title: '数据有误!',
                  })
                }
              }
            })
          }
        }
      })
    }
    

  

  },
  SubmitComment:function(e){
    var openId = wx.getStorageSync('useronlyData').openid;
    if( openId == undefined){
          wx.showToast({
            title: '请先登录',
            icon: 'none',
            duration: 3000
          })
    }else{
      var inputComment = e.detail.value.comment
      var value = inputComment.trim()
      var that = this
      if (value == "") {
        wx.showToast({
          title: '评论不能为空',
          icon: 'none',
          duration: 2000
        })
      } else {
        wx.request({
          url: 'http://localhost:8080/Comment/add2',
          method: 'GET',
          data: {
            openId: that.data.openId,
            articleId: that.data.currentPostId,
            comment: value


          },

          success: function (res) {
            var comment_List = res.data.commentList
            if (comment_List != null) {
              that.setData({
                commentList: comment_List
              })
            } else {

            }



          }

        })
      }
    }
    

  },



  //点赞功能
  onCollectionTip:function(event){
    var that = this 
    var openId = wx.getStorageSync('useronlyData').openid;
    if (openId == undefined) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 3000
      })
    } else {
   
    if(that.data.collected == false){
      wx.request({
        url: 'http://localhost:8080/ArticleStar/addStar?openId=' + that.data.openId + '&articleId=' + that.data.currentPostId,
        method: 'GET',
        data: {},
        success:function(res){
            var isSuccess = res.data.isSuccess;
            if(isSuccess){
              that.setData({
                collected : true
              })
            }
        }

      })
    }else{
      wx.request({
        url: 'http://localhost:8080/ArticleStar/removeStar?openId=' + that.data.openId + '&articleId=' + that.data.currentPostId,
        method: 'GET',
        data: {},
        success: function (res) {
          var isSuccess = res.data.isSuccess;
          if (isSuccess) {
            that.setData({
              collected: false
            })
          } 
        }

      })
    }
    
    //操作提示
    wx.showToast({
      title: that.data.collected ? '取消成功' : '点赞成功',
      icon:'success',
      duration:1000
    })
    
    }
  },

//分享功能
  onShareTap:function(event){
    //清除缓存
    //wx.removeStorageSync('key')
    //清除所有缓存
    //wx.clearStorageSync()
    var itemList=[
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博"
    ]
    wx.showActionSheet({
      itemList: itemList,
      itemColor:'#405f80',
      success:function(res){
          //res.tapIndex 数组元素的序号，从0开始
        if (res.tapIndex!=undefined){
            wx.showModal({
              title: '用户' + itemList[res.tapIndex],
              content: '用户是否取消？现在无法实现分享功能',
            })
          }
      }
    })
  },


  //转发
  onShareAppMessage: function (event) {
    return {
      title: this.data.article.articleTitle,
      path: '/pages/posts/post-detail/post-detail?id=' + this.data.currentPostId,
      success: function (res) {
        console.log(res)
        wx.getShareInfo()
      },
      fail: function () {
        console.log("转发失败")
      }
    }
  }
})